In a render.html file I have a button that calls a function in render.js (I included render.js as src in script)
<button type="button" onclick="someFunc()">Calculate</button>
In render.js someFunc() makes a request
options = {
method: "POST",
headers: {
"Content-Type" : "application/json"
},
body: JSON.stringify(feat1dataObj)
}
const test1 = await fetch('http://localhost:5000/analyzeInput', options)
app.py is the backend that should handle the request
from flask import Flask, jsonify, request
app = Flask(__name__);
@app.route('/analyzeInput', methods = ['POST'] )
def analyzeData(res):
part1Len = request.json['part1Len']
print(part1Len);
res = jsonify( {"Testing":"1"} )
res.headers.add('Access-Control-Allow-Origin', '*')
res.headers.set('Access-Control-Allow-Methods', 'GET, POST')
return res;
#start server
if __name__ == "__main__":
app.run(debug=True);
But I get 405 ""Method Not Allowed The method is not allowed for the requested URL"; the typical solution is that method = ["POST"] is not included but I have that.
Seems like I'm making a GET request somehow?
Overall, I'm using ElectronJS to make a GUI, just trying to pass data from the GUI to Python script to process, not sure why tutorials recommend hosting; seems like there should be a way a much simpler way to do this.
EDIT:
@app.route('/analyzeInput', methods = ['GET', 'POST', 'OPTIONS'] )
#def analyzeData(res):
#def analyzeData(request):
def analyzeData():
if request.method == 'POST':
print("entered analyzeInput")
#part1Len = request.json['part1Len']
#print(part1Len);
res = jsonify( {"Testing":"1"} )
res.headers.add('Access-Control-Allow-Origin', '*')
#res.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
res.headers.add('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
return res;
Code currently looks like this (I changed analyzeData to analyzeInput; and changed it everywhere so I don't worry about that part). Getting "view function for 'analyzeData' did not return a valid response. The function either returned None or ended without a return statement" as the internal server error.
Again, pretty sure that the problem is that the request is sent as "GET" not "POST" but cannot find an explanation for this. New output
It's cross site origin(CORS) issue. From your code I can see you have already taking care of that but in the app.route add options like this:
@app.route('/analyzeInput', methods = ['POST', 'OPTIONS'] ) . The reason is that the browser rewrites the request and changes the request method to options in compliance to the cross site origin(CORS) policy that took effect in 2014.
In addition to @john mba’s answer you should add mode: “cors” to your options